// Jeol Scan Viewer.cpp : Defines the class behaviors for the application.

#include "stdafx.h"
#include "JeolApp.h"
#include "JeolDlg.h"
//#include "CustomCtrl.h"
//#include "Preview1.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


BEGIN_MESSAGE_MAP(CJeolApp, CWinAppEx)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


CAutoPtr < ITextOutput > pLogger;
CConfiguration theConfig;
CJeolApp theApp;

CJeolApp::CJeolApp(void) : m_bLogging(FALSE)
{
	CAutoPtr < ITextOutput > p;
	p.Attach( new CDummyLogger() );
	p.m_p = (ITextOutput *)::InterlockedExchangePointer((void**)&pLogger.m_p, p.m_p);
	// 	pLogger = p;
}

CJeolApp::~CJeolApp(void)
{
}

void CJeolApp::StartLog(void)
{
	if ( !m_bLogging )
	{
		CAutoPtr < ITextOutput > p;
		p.Attach( new CLogger() );
		p.m_p = (ITextOutput *)::InterlockedExchangePointer((void**)&pLogger.m_p, p.m_p);
		// 	pLogger = p;
		m_bLogging = TRUE;
	}
}

void CJeolApp::StopLog(void)
{
	if ( m_bLogging )
	{
		CAutoPtr < ITextOutput > p;
		p.Attach( new CDummyLogger() );
		p.m_p = (ITextOutput *)::InterlockedExchangePointer((void**)&pLogger.m_p, p.m_p);
		// 	pLogger = p;
		m_bLogging = FALSE;
	}
}


BOOL CJeolApp::InitInstance()
{
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinAppEx::InitInstance();

	SetRegistryKey(_T("HiDev"));

	//VERIFY(CustomCtrlHlp::Register(RUNTIME_CLASS(CPreviewCtrl)));

	theConfig.Load(_T("config.txt"));

	m_pDlgWnd = new CJeolDlg;
	if (!m_pDlgWnd)
		return FALSE;

	if ( !m_pDlgWnd->Create(IDD_JEOLSCANVIEWER_DIALOG) )
	{
		delete m_pDlgWnd;
		m_pDlgWnd = NULL;
	}

	m_pMainWnd = m_pDlgWnd;

	return TRUE;
}

int CJeolApp::ExitInstance()
{
	if (m_pDlgWnd)
	{
		m_pDlgWnd->DestroyWindow();
		delete m_pDlgWnd;
		m_pDlgWnd = NULL;
		m_pMainWnd = NULL;
	}

	theConfig.Save();

	return CWinAppEx::ExitInstance();
}
